<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="header">
        <el-page-header :content="employeeForm.id ? '员工详情' : '添加员工' " @back="$router.back()" />
      </div>
      <div class="edit-form">
        <el-form ref="userForm" :model="employeeForm" :rules="employeeRules" label-width="220px">
          <!-- 姓名 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="employeeForm.username" class="inputW" maxlength="4" size="mini" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="employeeForm.workNumber" class="inputW" disabled size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机号 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="employeeForm.mobile" :disabled="!!employeeForm.id" class="inputW" maxlength="11" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--部门-->
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <cascader-tree v-model="employeeForm.departmentId" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--聘用形式-->
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="employeeForm.formOfEmployment" class="inputW" size="mini">
                  <el-option v-for="item in formOfEmployment" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!--入职日期-->
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="employeeForm.timeOfEntry"
                  class="inputW"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!--转正日期-->
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="employeeForm.correctionTime"
                  class="inputW"
                  size="mini"
                  type="date"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <upload-avatar v-model="employeeForm.staffPhone" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveEmployee">{{ employeeForm.id ? '保存更新' : '保存' }}</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import cascaderTree from '@/views/employee/components/cascader-tree.vue'
import UploadAvatar from '@/views/employee/components/upload-avatar.vue'
import { getEmployeeById, incrEmployee, updateEmployById } from '@/api/employee'

export default {
  name: 'Detail',
  components: { cascaderTree, UploadAvatar },
  data() {
    return {
      formOfEmployment: [{ id: 1, name: '正式' }, { id: 2, name: '非正式' }],
      employeeForm: {
        username: '', // 员工名字
        mobile: '', // 员工手机号
        formOfEmployment: null, // 员工聘用形式 1表示正式，2表示非正式
        workNumber: '', // 员工工号
        departmentId: null, // 员工部门id
        timeOfEntry: '', // 员工入职日期
        correctionTime: '', // 员工转正日期
        staffPhone: '' // 头像信息
      },
      employeeRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名字符长度为1-4位之间', trigger: 'blur' }
        ],
        workNumber: '',
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职日期', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正日期', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (this.employeeForm.timeOfEntry) {
                if (new Date(value) < new Date(this.employeeForm.timeOfEntry)) {
                  callback(new Error('转正时间不能小于入职时间'))
                } else {
                  callback()
                }
              }
            }
          }
        ],
        staffPhone: ''
      }
    }
  },
  created() {
    this.getEmployeeInfoById()
  },
  methods: {
    // 添加员工
    saveEmployee() {
      this.$refs.userForm.validate(async success => {
        if (success) {
          // 校验通过，发起新增员工接口 判断是添加员工还是编辑员工，根据员工id
          if (!this.employeeForm.id) {
            await incrEmployee(this.employeeForm)
          } else {
            await updateEmployById(this.employeeForm)
          }
          // 提示成功信息
          this.$message.success(`${this.employeeForm.id ? '更新员工信息成功' : '新增员工成功'}`)
          // 清空表单
          this.$refs.userForm.resetFields()
          // 返回员工列表
          this.$router.push('/employee')
        }
      })
    },
    // 根据员工id获取员工信息
    async getEmployeeInfoById() {
      if (this.$route.params.id) {
        this.employeeForm = await getEmployeeById(this.$route.params.id)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 60px;
  display: flex;
  align-items: center;
  margin-left: 60px;
}

.edit-form {
  background: #fff;
  padding: 20px;

  .inputW {
    width: 380px
  }

  .avatar-uploader ::v-deep .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader ::v-deep .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    position: absolute;
    top: 0;
    left: 0;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;

    &[src=""], &:not([src]) {
      opacity: 0;
      visibility: hidden;
      border: none;
      max-width: none;
    }

  }
}

</style>
